package org.ifilm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ifilm.model.UserJobPosting;
import org.ifilm.model.User;
import org.ifilm.service.NoticeService;
import org.ifilm.util.GeneralConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * Notice Contoller. All requests about notices will be here
 * @author Mustafa Genç
 *
 */
@Controller
@RequestMapping("/notice/**")
public class NoticeContoller {
	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping(value = "/createJobNotice", method = {RequestMethod.POST, RequestMethod.GET})
	public void createJobNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//TODO
		
		response.sendRedirect("/film/index.jsp");
	}
	
	@RequestMapping(value = "/listJobPostings", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView listFilms(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView result = new ModelAndView("/listJobPostings.jsp");
		
		User loggedUser = getLoggedUser(request);
		
		List<UserJobPosting> postingList = new ArrayList<UserJobPosting>();
		
		if (loggedUser != null) {
			postingList = noticeService.listJobPostings(loggedUser);
		}
		
		result.addObject("postings", postingList);
		
		return result;
	}
	
	@RequestMapping(value = "/createEquipmentNotice", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView createEquipmentNotice(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("/index.jsp");
		
		//TODO
		
		return result;
	}
	
	private User getLoggedUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		return (User) session.getAttribute(GeneralConstants.user);
	}
}
